<template>
    <div style="width:100%;height:100%;overflow:hidden auto">
        <weui-input :list="input_0_list" title="" more_label="" :tips="null" agreement_name="">
            <weui-button slot="button" type="primary" @on-click="onSend">
                发放
            </weui-button>
        </weui-input>
        <weui-cell :list="cell_0_list">
        </weui-cell>
    </div>
</template>

<script>
import WeuiInput from '../vue2componentSet/src/components/WeuiInput'
import WeuiButton from '../vue2componentSet/src/components/WeuiButton'
import WeuiCell from '../vue2componentSet/src/components/WeuiCell'
import global from '../lib/global'
import axios from 'axios'

export default {
    components: {
        WeuiInput,
        WeuiButton,
        WeuiCell
    },
    data() {
        return {
            input_0_list: [{
                label: '活动名称',
                value: ''
            }, {
                label: '活动开始时间',
                value: ''
            }, {
                label: '活动结束时间',
                value: ''
            }, {
                label: '红包个数',
                value: ''
            }, {
                label: '红包金额',
                value: ''
            }],
            cell_0_list: [],
            activityId: ''
        }
    },
    mounted() {
        location.hash.split('?')[1] && location.hash.split('?')[1].split('&').forEach(kv => {
            this[kv.split('=')[0]] = decodeURIComponent(kv.split('=')[1])
        })

        this.$store.commit('set_title', 'activity config')

        if (this.activityId) {
            axios.post(global.server + 'getActivityInfo.do', {
                activityId: this.activityId
            }).then(result => {
                const obj = result.data
                if (obj.result == 1) {
                    this.$set(this.input_0_list[0], 'value', obj.activityInfo.name)
                    this.$set(this.input_0_list[1], 'value', obj.activityInfo.startTime)
                    this.$set(this.input_0_list[2], 'value', obj.activityInfo.endTime)
                    this.$set(this.input_0_list[3], 'value', obj.activityInfo.num)
                    this.$set(this.input_0_list[4], 'value', obj.activityInfo.money)

                    if (obj.activityInfo.customers) {
                        obj.activityInfo.customers.forEach(item => {
                            this.cell_0_list.push({
                                value: item.customer_phone,
                                desc: item.money
                            })
                        })
                    }
                } else if (obj.result == 0) {
                    this.$router.goback()
                }
            }, result => {})
        }
    },
    methods: {
        onSend() {
            axios.post(global.server + 'sendWechatRedPack.do', {
                activityId: this.activityId,
                name: this.input_0_list[0].value,
                startTime: this.input_0_list[1].value,
                endTime: this.input_0_list[2].value,
                num: this.input_0_list[3].value,
                money: this.input_0_list[4].value
            }).then(result => {
                this.$store.commit('loading', {
                    is_show: false
                })
                const obj = result.data
                if (obj.result == 1) {
                    this.$store.commit('toast', {
                        content: '成功',
                        type: 'success'
                    })
                } else if (obj.result == 0) {
                    this.$router.goback()
                }else {
                    this.$store.commit('toast', {
                        content: '失败',
                        type: 'warn'
                    })
                }
            }, result => {
                this.$store.commit('loading', {
                    is_show: false
                })
            })
        }
    }
}
</script>